﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uppg5
{
    public class HashTable <TKey, TValue>
    {
        private Node<TKey, TValue>[] array;

        public HashTable(int size = 8)
        {
            array = new Node<TKey, TValue>[size];
        }

        public void Add(TKey key, TValue value)
        {
            int hash = Math.Abs(key.GetHashCode() % array.GetLength(0));
            Node<TKey, TValue> n = new Node<TKey, TValue>(key, value);
            n.Next = array[hash];
            array[hash] = n;
        }

        public Node<TKey, TValue> Search(TKey key)
        {
            int hash = Math.Abs(key.GetHashCode() % array.GetLength(0));
            Node<TKey, TValue> n = array[hash];
            while(n != null && !n.Key.Equals(key))
            {
                n = n.Next;
            }
            return n;
        }

        public void Print()
        {
            foreach (Node<TKey, TValue> n in array)
            {
                Node<TKey, TValue> n2 = n;
                while(n2 != null)
                {
                    Console.WriteLine(n2.Key + " : " + n2.Value);
                    n2 = n2.Next;
                }
            }
        }
    }
}
